﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Wolfy.Angularjs_Mvc_SPA.Models;

namespace Wolfy.Angularjs_Mvc_SPA.Controllers.api
{
    public class ProductController : ApiController
    {
        private ListDb<Product> _db = null;
        HttpResponseMessage response = null;
        public ProductController()
        {
            response = new HttpResponseMessage(HttpStatusCode.ResetContent);
            response.Headers.Add("Access-Control-Allow-Origin", "*"); //允许哪些url可以跨域请求到本域
            response.Headers.Add("Access-Control-Allow-Methods", "POST"); //允许的请求方法，一般是GET,POST,PUT,DELETE,OPTIONS
            response.Headers.Add("Access-Control-Allow-Headers", "x-requested-with,content-type"); //允许哪些请求头可以跨域
            _db = ListDb<Product>.CreateList();


        }
        // GET: api/Order
        [HttpGet]
        [Route("api/product/lists")]
        public async Task<HttpResponseMessage> Get(string key, int page)
        {
            return await Task.Factory.StartNew(() =>
            {
                int pageSize = 10;
                //总页数
                int pageCount = (int)Math.Ceiling(_db.Lst.Count * 1.0 / pageSize);
                IEnumerable<Product> pagedResults = new List<Product>();
                if (page <= pageCount)
                {
                    var results = string.IsNullOrEmpty(key) ? _db.Lst : _db.Lst.Where(x => x.Name.Contains(key)).OrderByDescending(x => x.Dt).ToList();
                    //分页
                    pagedResults = results.Skip((page - 1) * pageSize).Take(pageSize);
                }


                response.StatusCode = HttpStatusCode.OK;
                response.Content = new StringContent(JsonConvert.SerializeObject(new { _code = 200, _data = pagedResults }));
                return response;
            });
        }
        [HttpPost]
        [Route("api/product/lists")]
        public async Task<HttpResponseMessage> Post([FromBody]Product order)
        {
            return await Task.Factory.StartNew(() =>
            {
                //模拟id
                order.Id = new Random().Next(101, 5000);
                _db.Lst.Add(order);
                response.StatusCode = HttpStatusCode.OK;
                response.Content = new StringContent(JsonConvert.SerializeObject(new { _code = 200, _data = order }));
                return response;
            });
        }
        [HttpPut]
        [Route("api/product/lists")]
        // POST: api/Order
        public async Task<HttpResponseMessage> Put([FromBody] Product order)
        {
            return await Task.Factory.StartNew(() =>
            {
                var find = _db.Lst.Find(o => o.Id == order.Id);
                if (find != null)
                {
                    find.Name = order.Name;
                    find.Price = order.Price;
                    find.Dt = order.Dt;
                    response.StatusCode = HttpStatusCode.OK;
                    response.Content = new StringContent(JsonConvert.SerializeObject(new { _code = 200, _data = find }));
                }


                return response;
            });
        }

        [HttpDelete]
        [Route("api/product/lists")]
        // DELETE: api/Order/5
        public async Task<HttpResponseMessage> Delete(int id)
        {
            return await Task.Factory.StartNew(() =>
            {
                Product findOder = _db.Lst.Find(o => o.Id == id);
                _db.Lst.Remove(findOder);
                response.StatusCode = HttpStatusCode.OK;
                response.Content = new StringContent(JsonConvert.SerializeObject(new { _code = 200, _data = _db }));
                return response;
            });


        }
    }
}
